<template>
  <div class="app">
    <el-form :inline="true" :model="queryForm" @keyup.enter.native="m_onSubmit">
      <el-form-item label="商品名称">
        <el-input
          v-model="queryForm.query"
          placeholder="商品名称"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="m_onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-row class="p-b">
      <el-col>
        <el-button
          v-permission="['goods_105_03']"
          @click="addGood"
        >添加</el-button>
        <el-button @click="m_exportData">导出</el-button>
      </el-col>
    </el-row>
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column prop="goods_id" label="商品 ID" min-width="80" fixed />
      <el-table-column prop="goods_name" label="商品名称" min-width="200" />
      <el-table-column prop="goods_price" label="价格" min-width="80" />
      <el-table-column prop="goods_number" label="数量" min-width="80" />
      <el-table-column prop="goods_weight" label="重量" min-width="80" />
      <el-table-column label="状态" min-width="80">
        <template slot-scope="scope">
          {{ goodStatus[scope.row.goods_state] }}
        </template>
      </el-table-column>
      <el-table-column label="添加时间" min-width="150">
        <template slot-scope="scope">
          {{ scope.row.add_time | parseTime }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" min-width="150">
        <template slot-scope="scope">
          {{ scope.row.upd_time | parseTime }}
        </template>
      </el-table-column>
      <el-table-column prop="hot_mumber" label="热销品数量" min-width="100" />
      <el-table-column label="是否是热销品" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.is_promote ? '是': '否' }}
        </template>
      </el-table-column>
      <el-table-column width="80" label="操作" fixed="right">
        <template slot-scope="scope">
          <el-dropdown>
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="editUser(scope.row.id)"
              >编辑</el-dropdown-item>
              <el-dropdown-item @click.native="putUserState(scope.row)">
                <span>{{
                  scope.row.mg_state ? "禁用" : "启用"
                }}</span></el-dropdown-item>
              <el-dropdown-item
                @click.native="editUserRole(scope.row)"
              >角色</el-dropdown-item>
              <el-dropdown-item
                @click.native="deleteUser(scope.row)"
              >删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="m_pagination.total"
      :page.sync="m_pagination.page"
      :limit.sync="m_pagination.pageSize"
      @pagination="loadTableData"
    />
    <user-form
      :id="userId"
      :visible.sync="dialogVisible"
      :type="type"
      :cb="loadTableData"
    />
    <role-form
      :visible.sync="roleVisible"
      :user="user"
      :cb="loadTableData"
    />
  </div>
</template>

<script>
import { getGoods } from '@/api/goods'
import { excel, pagination, query } from '@/mixins'
import { parseTime } from '@/utils'
// import importAll from '@/utils/importAll'
// const components =  importAll(require.context('./components', true, /\.vue$/))

// 导出 excel
const tHeader = ['用户名', '角色', '状态', '电话', '邮箱', '注册时间']
const filterVal = [
  'username',
  'role_name',
  'mg_state',
  'mobile',
  'email',
  'create_time'
]
const excelName = '用户列表'

const goodStatus = {
  0: '未通过',
  1: '审核中',
  2: '已审核'
}

export default {
  name: 'GOODLIST',
  // components: {
  //   ...components
  // },
  mixins: [
    pagination(),
    query(),
    excel(tHeader, filterVal, excelName, 'excelData')
  ],
  data() {
    return {
      queryForm: {
        query: ''
      },
      tableData: [],
      dialogVisible: false,
      type: 1,
      userId: '',
      roleVisible: false,
      user: {},
      excelData: [],
      goodStatus
    }
  },
  created() {
    this.loadTableData()
  },
  methods: {
    async loadTableData() {
      const params = {
        query: this.queryForm.query,
        pagenum: this.m_pagination.page,
        pagesize: this.m_pagination.pageSize
      }
      const { goods, total } = await getGoods(params)
      this.tableData = goods
      this.m_pagination.total = total
      this.getExcelData()
    },
    addGood() {
      this.$router.push({ name: 'goods_add' })
    },
    async deleteUser(user) {
      this.$confirm(`删除${user.username}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteUser(user.id)
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.loadTableData()
        })
        .catch(() => {
          console.log('取消删除')
        })
    },
    editUser(id) {
      this.type = 2
      this.userId = id
      this.dialogVisible = true
    },
    async putUserState(user) {
      console.log('user:', user)
      await putUserState(user)
      this.$message({
        message: '更改成功',
        type: 'success'
      })
      this.loadTableData()
    },
    editUserRole(user) {
      this.user = user
      this.roleVisible = true
    },
    getExcelData() {
      this.excelData = this.tableData.map(i => {
        i.mg_state = i.mg_state ? '启用' : '禁用'
        i.create_time = parseTime(i.create_time)
        return i
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
